<?php
defined('IN_TS') or die('Access Denied.');
 
class message extends tsApp{

	//构造函数
	public function __construct($db){
        $tsAppDb = array();
		include 'app/message/config.php';
		//判断APP是否采用独立数据库
		if($tsAppDb){
			$db = new MySql($tsAppDb);
		}
	
		parent::__construct($db);
	}
	

    /**
     * 	发送消息
     * @param $userid       发送者用户ID，0为系统消息
     * @param $touserid     接收消息的用户ID
     * @param $content      消息内容
     * @param string $tourl 消息对应的内容网址
     */
    public function sendmsg($userid,$touserid,$content,$tourl=''){
	
		$userid = intval($userid);
		$touserid = intval($touserid);
		$content = tsClean(trim($content));
		
		if($touserid && $content){
		
			$messageid = $this->create('message',array(
				'userid'=> $userid,
				'touserid'=> $touserid,
				'content'=> $content,
                'tourl'=>$tourl,
				'addtime'=> time(),
			));
			
		}
		if($userid==0){
			$arr = array('subject'=>'系统通知');
			$url = tsUrl('message','system');
		}else{
			$arrUser = aac('user')->find('user_info',array('userid'=>$userid),'username');
			$arr = array('subject'=>'来自'.$arrUser['username'].'的邮件');
			$url = tsUrl('message','user',array('touserid'=>$userid));
		}

		aac('wechat')->sendwechat(1,$touserid,$arr,$url);

		return true;
	}
	
	
}